<template>
    <div>
<el-dialog :title="isAdd? '添加管理员' :  '编辑管理员' " :visible.sync="isShow" :before-close='cancel'>
         <el-form :model="conservatorFrom" :rules='rules' ref="conservatorRef">

    <el-form-item label="所属角色" :label-width="formLabelWidth" prop="roleid">
      <el-select v-model="conservatorFrom.roleid" placeholder="请选择" >
        <el-option
       v-for="item in $store.getters['role/getRole']"
      :key="item.id"
      :label="item.rolename"
      :value="item.id"
      ></el-option>

      </el-select>
    </el-form-item>

  <el-form-item label="用户名称" :label-width="formLabelWidth" prop="username">
      <el-input v-model="conservatorFrom.username" autocomplete="off"></el-input>
    </el-form-item>

<el-form-item label="密码" :label-width="formLabelWidth" prop="password">
      <el-input v-model="conservatorFrom.password" autocomplete="off" type='password'></el-input>
    </el-form-item>
     <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="conservatorFrom.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer" >
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="add">确 定</el-button>
  </div>
</el-dialog></div>
</template>

<script>

import { useradd, userinfo, useredit } from '../../api/api.js'
import { mapActions } from 'vuex'
export default {
  props: ['isShow', 'isAdd'],
  data () {
    return {
      formLabelWidth: '120px',
      conservatorFrom: {
        roleid: '', // 角色编号
        username: '', // 管理员名称
        password: '', // 密码
        status: 1
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        roleid: [
          { required: true, message: '选择所属角色', trigger: 'change' }
          //   { min: 1, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  mounted () {
    if (this.$store.getters['role/getRole'].length === 0) {
      this.$store.dispatch('role/getRolelist')
    }
  },
  methods: {
    ...mapActions('conservator', ['getUsercount']),
    add () {
      this.$refs.conservatorRef.validate((vali) => {
        if (vali) {
          if (this.isAdd) {
            useradd(this.conservatorFrom).then(res => {
              if (res.code === 200) {
                this.$store.dispatch('conservator/getUserlist')
                this.$message.success(res.msg)
                this.cancel()
                this.getUsercount()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            // 编辑
            console.log(this.conservatorFrom)
            useredit(this.conservatorFrom).then(res => {
              console.log(res)
              if (res.code === 200) {
                // 重新调用列表
                this.$store.dispatch('conservatorFrom/getUserlist')
                this.$message.success(res.msg)
                this.cancel()
              } else {
                this.$message.error(res.msg)
              }
            })
          }

          // 菜单添加接口
        }
      })
    },
    reset () {
      this.conservatorFrom = {
        roleid: '', // 角色编号
        username: '', // 管理员名称
        password: '', // 密码
        status: 1
      }
      this.$refs.conservatorRef.resetFields()
    },
    cancel () {
      this.$emit('cancel', false)
      this.reset()
    },
    async  lookup (uid) {
      const res = await userinfo({ uid })
      this.conservatorFrom = res.list
      this.conservatorFrom.uid = uid
      console.log(this.conservatorFrom)

      this.conservatorFrom.password = ''
    }
  }
}
</script>

<style>

</style>
